<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * 支付成功发送订单信息模版函数
 * @param  Decimal $openid    微信用户Openid
 * @param  Decimal $template  微信模版ID
 * @param  Decimal $order_sn  订单支付单号
 * @param  Decimal $latitude2  终点纬度
 * @param  Int     $unit       单位 1:米 2:公里
 * @param  Int     $decimal    精度 保留小数位数
 * @return Decimal
 */
function sent_weixin_template_message($openid, $templateID, $order_sn) {

   $order = D('order');
   $map['pay_sn'] = $order_sn;
   $list = $order->relation('OrderGoods')->where($map)->select();
   foreach ($list as $value) {
       foreach ($value['OrderGoods'] as $v) {
           
       }
   }
   
   
    $template = array('touser' => $openid,
        'template_id' => $templateID,
        'url' => "http://weixin.taodudu.com.cn/order",
        'topcolor' => "#7b68ee",
        'data' => array('first' => array('value' => urlencode("您好,您购买的“淘嘟嘟”商品信息如下："),
                'color' => "#000000",
            ),
            'orderProductName' => array('value' => '1kg德运奶粉+800g卡乐比麦片',
                'color' => "#000000",
            ),
            'orderMoneySum' => array('value' => $notify->data['cash_fee'] / 100,
                'color' => "#ff0000",
            ),
            'remark' => array('value' => urlencode("\\n您的订单已提交，我们将尽快为您发货！"),
                'color' => "#000000",
            ),
        )
    );
    $result = urldecode(json_encode($template));
    $kefu = new \Libs\WeiXin\CustomService();
    $kefu->send_template_message($result);
}



  /**
     * 生成单量数据
     * @param int $num 数量
     * @param int $type 类型
     * @return array 信息
     */
    function getJson($num , $type = 'day', $date = 'Y-m-d'){
        $jsonArray = array();
        $jsonArray['labels'] = array();
        $datasets = getChart('green');
        $datasets['label'] = '销量';
        $timeArray = array();
        for ($i=0; $i < $num; $i++) {
            $timeNow = strtotime("-".$i." ".$type,strtotime(date('Y-m-d 0:0:0')));
            $jsonArray['labels'][] = date($date,$timeNow);
            $where = array();
            $where['add_time'] = array(array('EGT', $timeNow), array('LT', strtotime('+ 1 '.$type, $timeNow)),'and');
            $where['order_state'] = array('EGT',20);
            $sum = M('order')->where($where)->count();
            if($sum){
                $datasets['data'][] = $sum;
            }else{
                $datasets['data'][] = 0;
            }
        }
        $jsonArray['labels'] = array_reverse($jsonArray['labels']);
        $datasets['data'] = array_reverse($datasets['data']);
        $jsonArray['datasets'][] = $datasets;
        return json_encode($jsonArray);
    }

  /**
     * 生成单量数据
     * @param int $num 数量
     * @param int $type 类型
     * @return array 信息
     */
    function getUserJson($num , $type = 'day', $date = 'Y-m-d'){
        $jsonArray = array();
        $jsonArray['labels'] = array();
        $datasets = getChart('green');
        $datasets['label'] = '用户';
        $timeArray = array();
        for ($i=0; $i < $num; $i++) {
            $timeNow = strtotime("-".$i." ".$type,strtotime(date('Y-m-d 0:0:0')));
            $jsonArray['labels'][] = date($date,$timeNow);
            $where = array();
            $where['user_time'] = array(array('EGT', $timeNow), array('LT', strtotime('+ 1 '.$type, $timeNow)),'and');
            $sum = M('users')->where($where)->count();
            if($sum){
                $datasets['data'][] = $sum;
            }else{
                $datasets['data'][] = 0;
            }
        }
        $jsonArray['labels'] = array_reverse($jsonArray['labels']);
        $datasets['data'] = array_reverse($datasets['data']);
        $jsonArray['datasets'][] = $datasets;
        return json_encode($jsonArray);
    }

  /**
     * 生成单量数据
     * @param int $num 数量
     * @param int $type 类型
     * @return array 信息
     */
    function getViewJson($num , $type = 'day', $date = 'Y-m-d'){
        $jsonArray = array();
        $jsonArray['labels'] = array();
        $datasets = getChart('green');
        $datasets['label'] = '访问';
        $timeArray = array();
        for ($i=0; $i < $num; $i++) {
            $timeNow = strtotime("-".$i." ".$type,strtotime(date('Y-m-d 0:0:0')));
            $jsonArray['labels'][] = date($date,$timeNow);
            $where = array();
            $where['time'] = array(array('EGT', $timeNow), array('LT', strtotime('+ 1 '.$type, $timeNow)),'and');
            $sum = M('visitor')->where($where)->getField('count');
            if($sum){
                $datasets['data'][] = $sum;
            }else{
                $datasets['data'][] = 0;
            }
        }
        $jsonArray['labels'] = array_reverse($jsonArray['labels']);
        $datasets['data'] = array_reverse($datasets['data']);
        $jsonArray['datasets'][] = $datasets;
        return json_encode($jsonArray);
    }
    
    /**
     * 图表样式
     * @param int $fragmentId ID
     * @return array 信息
     */
    function getChart($type)
    {
        $style = array();
        switch ($type) {

            case 'grey':
                $style['fillColor'] = "rgba(220,220,220,0.2)";
                $style['strokeColor'] = "rgba(220,220,220,1)";
                $style['pointColor'] = "rgba(220,220,220,1)";
                $style['pointStrokeColor'] = "#fff";
                $style['pointHighlightFill'] = "#fff";
                $style['pointHighlightStroke'] = "rgba(220,220,220,1)";
                break;
            case 'green':
                $style['fillColor'] = "rgba(102,205,170,0.2)";
                $style['strokeColor'] = "rgba(102,205,170,1)";
                $style['pointColor'] = "rgba(102,205,170,1)";
                $style['pointStrokeColor'] = "#fff";
                $style['pointHighlightFill'] = "#fff";
                $style['pointHighlightStroke'] = "rgba(102,205,170,1)";
                break;
            case 'orange':
                $style['fillColor'] = "rgba(233,150,122,0.2)";
                $style['strokeColor'] = "rgba(233,150,122,1)";
                $style['pointColor'] = "rgba(233,150,122,1)";
                $style['pointStrokeColor'] = "#fff";
                $style['pointHighlightFill'] = "#fff";
                $style['pointHighlightStroke'] = "rgba(233,150,122,1)";
                break;
            case 'blue':            
            default:
                $style['fillColor'] = "rgba(151,187,205,0.2)";
                $style['strokeColor'] = "rgba(151,187,205,1)";
                $style['pointColor'] = "rgba(151,187,205,1)";
                $style['pointStrokeColor'] = "#fff";
                $style['pointHighlightFill'] = "#fff";
                $style['pointHighlightStroke'] = "rgba(151,187,205,1)";
                break;
        }
        return $style;
    }


